package com.webdev.base.service.impl;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webdev.base.mapper.UserMapper;
import com.webdev.base.pojo.User;
import com.webdev.base.service.IUserService;
import com.webdev.base.util.Constants;
import com.webdev.base.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huangningshuang
 * @since 2021-12-13
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public User getUserByToken(String token) {
        if (token == null) return null;
//        return (User) redisTemplate.opsForValue().get(Constants.REDIS_USERTOKEN + token);
        Integer id = JWT.decode(token).getClaims().get(Constants.TOKEN_USERID).asInt();
        if (id == null) return null;
        log.info("user_id:" + id);
        return userMapper.getUserById(id);
    }

    @Override
    public void updateUserAddr(AddressVo addr, Integer uid) {
        userMapper.updateUserAddr(addr, uid);
    }

}
